import { createRequire } from 'module'
const require = createRequire(import.meta.url)
var gulp = require('gulp')
var sftp = require('gulp-sftp-up4')
const conf = require('./project.config.cjs')

const argsMap = {}
;(function (...args) {
  let args1 = args[0].slice(2)
  while (args1.length >= 0) {
    let key = args1.shift()
    if (!key) break
    key = key.replace(/\-/gi, '')
    if (key.indexOf('=') > -1) {
      key = key.split('=')
      argsMap[key[0]] = key[1]
    }
  }
  // eslint-disable-next-line no-undef
})(process.argv)
console.log(argsMap, conf)
const srcDist = ['./hqx/**']

gulp.task('land.yz', function (done) {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        host: '192.168.1.223',
        port: 22,
        user: 'root',
        pass: 'shipcat',
        remotePath: '/mnt/app/js/h5'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})
/**
 * http://land.test.ixunhang.com/
 */
gulp.task('land.test', function (done) {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        host: '36.133.76.226',
        auth: 'Server3',
        port: 23432,
        remotePath: '/data/vue/sturgeon/erptest/hqx'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})

gulp.task('land1', function (done) {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        ...conf[argsMap.type],
        remotePath: '/data/vue/sturgeon/erp/hqx'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})

gulp.task('land2', function (done) {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        ...conf[argsMap.type],
        remotePath: '/data/vue/sturgeon/single/h5'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})

gulp.task('ship.single', (done) => {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        ...conf[argsMap.type],
        remotePath: '/mnt/app/js/h5'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})

//  上传到 单机
gulp.task(
  'ship',
  gulp.series(gulp.parallel('land.yz', 'ship.single'), function (done) {
    console.log('船端上传完毕')
    done()
  })
)

//  同时执行 上传到 岸端 2个目录
gulp.task(
  'land',
  gulp.series(gulp.parallel('land1', 'land2', 'land.test'), function (done) {
    console.log('岸端上传完毕')
    done()
  })
)

//  上传到 生产
gulp.task('pro', function (done) {
  gulp
    .src(srcDist)
    .pipe(
      sftp({
        ...conf[argsMap.type],
        remotePath: '/data/vue/sturgeon/erp/hqx/'
      })
    )
    .on('error', () => {
      /* Ignore compiler errors */
    })

  done()
})
